package com.abc.controller;

import com.abc.annotation.PermInfo;
import com.abc.dao.SwiperMapper;
import com.abc.entity.Swiper;
import com.abc.service.SwiperService;
import com.abc.vo.Json;
import com.abc.vo.SwiperVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@PermInfo(value = "轮播模块", pval = "a:swiper")
@RequestMapping("/api/v1/swiper")
@RestController
public class SwiperController {

    private static final Logger log = LoggerFactory.getLogger(SwiperController.class);


    @Autowired
    SwiperMapper swiperMapper;

    @Autowired
    SwiperService swiperService;

    @PermInfo("轮播列表")
    @RequiresPermissions("a:swiper:list")
    @RequestMapping("/list")
    public Json query() {
        String oper = "list swiper";
        log.info("{}", oper);
        List<SwiperVo> list = swiperMapper.selectSwiperListWithImgPath();
        return Json.succ(oper, "list", list);
    }

    @PermInfo("轮播修改")
    @RequiresPermissions("a:swiper:update")
    @RequestMapping("/update")
    public Json upload(@RequestBody String body) {
        String oper = "update swiper";
        log.info("{}", oper);
        Swiper swiper = JSON.parseObject(body, Swiper.class);
        int count = swiperMapper.updateSwiper(swiper);
        return Json.succ(oper, "count", count);
    }

    @PermInfo("轮播全部刷新")
    @RequiresPermissions("a:swiper:flushAll")
    @RequestMapping("/flushAll")
    public Json flushAll(@RequestBody String body) {
        String oper = "flushAll swiper";
        log.info("{}", oper);
        List<Swiper> swiperList = JSON.parseArray(body, Swiper.class);
        swiperService.flushAll(swiperList);
        return Json.succ(oper);
    }

    @PermInfo("轮播增加")
    @RequiresPermissions("a:swiper:add")
    @RequestMapping("/add")
    public Json add(@RequestBody String body) {
        String oper = "add swiper";
        log.info("{}", oper);
        Swiper swiper = JSON.parseObject(body, Swiper.class);
        int count = swiperMapper.insertSwiper(swiper);
        return Json.succ(oper, "count", count);
    }

    @PermInfo("轮播删除")
    @RequiresPermissions("a:swiper:del")
    @RequestMapping("/del")
    public Json del(@RequestBody String body) {
        String oper = "del swiper";
        log.info("{}", oper);
        Swiper swiper = JSON.parseObject(body, Swiper.class);
        int count = swiperMapper.deleteById(swiper.getId());
        return Json.succ(oper, "count", count);
    }


}
